package cn.percent.parse;

import cn.percent.cache.Cache;
//import org.apache.ibatis.cache.Cache;
import cn.percent.cache.CacheBuilder;
import cn.percent.config.CacheConfig;
import cn.percent.config.Configuration;
import cn.percent.config.MappedStatement;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {
    private Configuration configuration;
    private CacheConfig cacheConfig;
    private boolean isCached = false;
    public XMLMapperBuilder(Configuration configuration){
        this.configuration = configuration;
        cacheConfig = new CacheConfig();

    }

    public void parse(InputStream inputStream) throws DocumentException, ClassNotFoundException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        List<Element> cacheList = rootElement.selectNodes("//cache");
        for (Element element : cacheList) {
            cacheConfig.setBlocking(element.attributeValue("blocking",null));
            cacheConfig.setEviction(element.attributeValue("eviction",null));
            cacheConfig.setFlushInterval(element.attributeValue("flushInterval",null));
            cacheConfig.setReadOnly(element.attributeValue("readOnly",null));
            cacheConfig.setSize(element.attributeValue("size",null));
            cacheConfig.setType(element.attributeValue("type",null));
        }
        if (cacheList != null) {
            CacheBuilder cacheBuilder = new CacheBuilder(cacheConfig);
            Cache cache = cacheBuilder.build();
            configuration.setCache(cache);
            this.isCached = true;
        }

        String namespace = rootElement.attributeValue("namespace");
        List<Element> list = rootElement.selectNodes("//select");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            boolean useCache = Boolean.valueOf(element.attributeValue("useCache"));
            boolean flushshCache = Boolean.valueOf(element.attributeValue("flushshCache"));
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            mappedStatement.setUseCache(useCache);
            mappedStatement.setFlushCache(flushshCache);
            String key = namespace + "." + id;
            if (this.isCached){
                mappedStatement.setCache(configuration.getCache());
            }
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        List<Element> updateList = rootElement.selectNodes("//update");
        for (Element element : updateList) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);

            String key = namespace + "." + id;
            if (this.isCached){
                mappedStatement.setCache(configuration.getCache());
            }
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        List<Element> deleteList = rootElement.selectNodes("//delete");
        for (Element element : deleteList) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key = namespace + "." + id;
            if (this.isCached){
                mappedStatement.setCache(configuration.getCache());
            }
            configuration.getMappedStatementMap().put(key,mappedStatement);

        }

        List<Element> insertList = rootElement.selectNodes("//insert");
        for (Element element : insertList) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key = namespace + "." + id;
            if (this.isCached){
                mappedStatement.setCache(configuration.getCache());
            }
            configuration.getMappedStatementMap().put(key,mappedStatement);

        }



    }
}
